<?php
$path = dirname(__FILE__);
require("$path/lib/crt.php");

$crt = new CRT();
$crt->init();

$user = $crt->getUser();
if($user->loggedIn) {
	header("Location: /index.php");
	exit;
}

if(isset($_REQUEST['loginSubmit'])) {
	$loginErr = true;

	$username = Clean::string($_REQUEST['username'], false, false, true);
	$password = Clean::string($_REQUEST['password'], false, false, true);

	if($username && $password) {
		$user = CRTUser::logIn($username, $password);

		if($user) {
			$loginErr = false;
			header('Location: /'); 
			exit;
		}
	}
}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <title>HQ CRT</title>
        <?php
        $dir = opendir(PLUGIN_PATH.'/css');
        while(($file= readdir($dir))!==false) {
            echo '<link rel="stylesheet" href="/css/'.$file.'" type="text/css" media="all">';
        }

        ?>
    </head>

    <body>

	<div id='loginWrap'>
		<form method='post' action='/user/login.php'>
			<h2>CRT Login</h2>
			<table>
				<?php if($loginErr):?><tr><td colspan=2><span style='color:red; font-weight: bold;'>Invalid user/pass combination</span></tr></td><?php endif; ?>
				<tr><td><label>Username:</label></td><td><input type='text' name='username' value='' /></td></tr>
				<tr><td><label>Password:</label></td><td><input type='password' name='password' value='' /></tr>
				<tr><td colspan=2>&nbsp;</td></tr>
				<tr><td colspan=2><input type='submit' name='loginSubmit' value='Submit' /></td></tr>
			</table>
		</form>
	</div>

    </body>
</html>
